/**
 * 
 */
/*pager 使用说明:
1.使用angular directive定义功能
2.uppager 指令为属性指令,定义在分页功能父级节点
3.必须为$scope定义名为:onSelectPage的方法, 会在用户进行翻页操作时回调
4.页面初始化时,获取数据后,需设置$scope.recordCount 对分页控件进行初始化
5.页面初始化时,需设置$scope.pageSize 属性
 */
var pagerFun = function () {
    var pager = {
        pageIndex: 1,
        pageSize: 10,
        pageCount: 0,
        recordCount: 0,
        isShowPager: true,
        pages: [],
        onSelectPage: function (page) {
            // 等待被外部逻辑覆盖
            alert("请为$scope定义名为:onSelectPage的方法");
        },
        isPoint: function (page) {
            return page == "…" || page == "...";
        },
        isActive: function (page) {
            return this.pageIndex == page;
        },
        selectPage: function (page) {
            if (!this.isActive(page) && !this.isPoint(page)) {
                this.pageIndex = page;
                this.onSelectPage(page);
                this.loadPage();
            }
        },
        previous: function () {
            if (!this.noPrevious()) {
                this.selectPage(1);
            }
        },
        last: function () {
            if (!this.noNext()) {
                this.selectPage(this.pageCount);
            }
        },
        selectPrevious: function () {
            if (!this.noPrevious()) {
                this.selectPage(this.pageIndex - 1);
            }
        },
        selectNext: function () {
            if (!this.noNext()) {
                this.selectPage(this.pageIndex + 1);
            }
        },
        noPrevious: function () {
            return this.pageIndex == 1;
        },
        noNext: function () {
            return this.pageIndex == this.pageCount;
        },
        loadPage: function () {
            this.pages = [];
            this.pageCount = Math.ceil(this.recordCount / this.pageSize);
            if (this.pageIndex < 1) {
                this.pageIndex = 1;
            } else if (this.pageIndex > this.pageCount) {
                this.pageIndex = this.pageCount;
            }
            ;
            if (this.pageIndex <= 5) {
                for (var i = 1; i < this.pageIndex; i++) {
                    this.pages.push(i);
                }
            } else {
                for (var i = 1; i < 3; i++) {
                    this.pages.push(i);
                }
                ;
                this.pages.push("…");

                for (var i = this.pageIndex - 2; i < this.pageIndex; i++) {
                    this.pages.push(i);
                }
                ;
            }
            ;
            this.pages.push(this.pageIndex);
            if (this.pageIndex + 3 >= this.pageCount) {
                for (var i = this.pageIndex + 1; i <= this.pageCount; i++) {
                    this.pages.push(i);
                }
            } else {
                for (var i = this.pageIndex + 1; i < this.pageIndex + 3; i++) {
                    this.pages.push(i);
                }
                this.pages.push('...');
            }
        },
        go: function (page) {
            if (page) {
                this.selectPage(Number(page));
            }
        }
    };

    return function (scope, element, attrs) {
        scope.$watch('recordCount', function (value) {
            if (value && value != 0) {
                pager.isShowPager = true;
                pager.recordCount = value;
                pager.loadPage();
                scope.pager = pager;
            } else {
                pager.isShowPager = false;
            }
        });
        scope.$watch("go", function (newVal, oldVal) {
            if (newVal) {
                if (newVal.indexOf('.') > -1) {
                    scope.go = oldVal;
                }
                else {
                    var currVal = Number(newVal);
                    if (isNaN(currVal)) {
                        scope.go = oldVal;
                    }
                    else {
                        if (currVal <= 0) {
                            scope.go = 1;
                        }
                        else if (currVal > pager.pageCount) {
                            scope.go = pager.pageCount;
                        }
                    }
                }
            }
        });
        pager.onSelectPage = scope.onSelectPage;
        pager.pageSize = scope.pageSize;
    };
};
if (typeof agentApp != "undefined") {
    agentApp.directive('uppager', pagerFun);
}
if (typeof corpApp != "undefined") {
    corpApp.directive('uppager', pagerFun);
}